<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML>
<html style='height:100%;'>
<head>
<base href="<%=basePath%>">

<title>特定事件新增</title>
<link rel="stylesheet" href="css/vivo.css">
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="js/bootstrap2/css/bootstrap.min.css">
<link rel="stylesheet" href="css/common.css">
<link href="js/select2/select2.min.css" type="text/css" rel="stylesheet" />
<link rel="stylesheet" type="text/css"
	href="static/p_house/house_index.css" />
<link rel="stylesheet" type="text/css"
	href="js/webuploader-0.1.5/webuploader.css" />

<script src="js/jquery-1.11.1.js"></script>
<script src="js/bootstrap2/js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/layui-v2.1.5/layui/layui.all.js"></script>
<script type="text/javascript"
	src="<%=basePath%>js/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src="js/webuploader-0.1.5/webuploader.js"></script>
<script type="text/javascript" src="js/sharefunction.js"></script>
<script src="js/select2/select2.min.js"></script>
<script type="text/javascript" src="js/form.js"></script>
<!-- 图片放大 -->
<script src="js/postbird-img-glass/postbird-img-glass.js"></script>
<style>
.webuploader-container {
	position: relative;
	top: 5px;
}
</style>
</head>
<script type="text/javascript">
	var eventFlag; //查看状态
	$(function() {
		getFiles();
		//初始化表单
		mySubmit("eventAddForm", initEventData, eventAddSuccess);
		initWebUpload("filePicker_erul", "list_erul", "资料上传", "eventRecord_articleId", planStageSuccess, planStageErr);
		//上传按钮添加默认宽高
		$("div[id^='filePicker_'] div").css({
			"width" : "60px",
			"height" : "28px"
		});

		initEventType();
		//获取编辑页面数据
		initEventsMsg();
	});
	function getFiles() {
		getFileListByFlag($("#eventAddForm input[name='eventId']").val(), "资料上传",1, "#list_erul", setRFL, imgPlus);
	}
	function imgPlus(data) {
		$("#eventAddForm .uploadcancel").css("display", "none");
		$.each(data, function(i, v) {
			//图片放大
			PostbirdImgGlass.init({
				domSelector : "#" + v.id + " img",
				animation : true,
				height : '80%',
				myDocument : window.parent.parent.parent
			});
		});

	}
	var rFileList = new Array(); //
	//设置待删除附件的值到集合中
	function setRFL(id, _this) {
		layer.confirm('删除该图片？', {
			title : '警告',
			icon : 0,
			shade : [ 0.1, '#fff' ]
		}, function() {
			rFileList.push(id);
			//删除指定缩略图
			$("#" + id).remove();
			layer.close(layer.index);
		});
	}
	//初始化赋值
	function initEventsMsg() {
		var eventId = $("#eventAddForm").find("input[name='eventId']").val();
		if (eventId == "") {
			//重置表单
			$("#eventAddForm")[0].reset();
			$("#eventAddForm #btn_div2").hide();
			$("#eventAddForm #btn_div1").show();
			$("#eventAddForm .uploadcancel").show();
			$("#eventAddForm div[id^='filePicker_']").css("display", "inline-block");
		} else {
			$.post("customer/event/getEventById", {
				id : eventId
			}, function(data, status) {
				if (data != null) {
					eventFlag = '${eventFlag}';
					$("#eventAddForm [name='eventType']").val(data.eventType).trigger("change");
					$("#eventAddForm input[name='eventDate']").val(new Date(data.eventDate).format('yyyy-MM-dd'));
					$("#eventAddForm [name='eventContent']").val(data.eventContent);
					$("#eventAddForm [name='eventProcess']").val(data.eventProcess);
					$("#eventAddForm input[name='eventParticipant']").val(data.eventParticipant);
					//隐藏按钮
					if (eventFlag == "1") {
						$("#eventAddForm #btn_div2").hide();
						$("#eventAddForm #btn_div1").hide();
						$("#eventAddForm").find("input,textarea").attr("readonly", true);
						$("#eventAddForm").find(".addbtn,select,.Wdate").attr("disabled", true);
						$("#eventAddForm div[id^='filePicker']").css("display", "none");
					} else {
						$("#eventAddForm #btn_div2").show();
						$("#eventAddForm #btn_div1").hide();
						$("#eventAddForm .uploadcancel").show();
						$("#eventAddForm #changeRemark").show();
						$("#eventAddForm div[id^='filePicker']").css("display", "inline-block");
					}

				}

			});
		}
	}

	var optEvent;
	//optEvent操作字段，0：临时保存；1：新增；2：修改
	function saveEvent(val) {
		optEvent = val;
		if (val == 3) {
			//解除锁定状态
			var index = layer.confirm('确定开启编辑状态？', {
				offset : '20%',
				shade : 0
			}, function() {
				//去除只读开启按钮编辑
				$("#eventAddForm").find("input,textarea").attr("readonly", false);
				$("#eventAddForm").find("select,.addbtn,.Wdate").attr("disabled", false);
				$("#eventAddForm").find(".Wdate").attr("readonly", true);
				$("#eventAddForm #modifyBtn").hide();
				$("#eventAddForm .hidbtn").addClass("showbtn");
				$("#eventAddForm .uploadcancel").show();
				$("#eventAddForm div[id^='filePicker']").unbind("click").show();
				$("#eventAddForm #changeRemark").show();
				$("div[id^='filePicker_'] div").css({
					"width" : "60px",
					"height" : "28px"
				});
				layer.close(index);
			});
		} else {

			$("#eventAddForm").submit();
		}
	}

	//获取事件列表
	function initEventType() {
		$.ajax({
			url : "customer/event/getEventType", //请求的url地址
			dataType : "json", //返回格式为json
			async : false, //请求是否异步，默认为异步，这也是ajax重要特性 false:同步  true：异步
			type : "post", //请求方式
			success : function(data) {
				var html = "";
				var $ts2 = $("#eventAddForm").find("[name='eventType']");
				$.each(data, function(i, v) {
					html = "<option value=\"" + v + "\">" + v + "</option>";
					$ts2.append(html);
				});

				//数据请求成功后初始化事件类型下拉
				$ts2.select2({
					tags : true
				});
			}
		});


	}
	var eventi;
	function initEventData() {
		eventi = layer.load(2);
		var fkcustomerId = parent.parent.$("#customerId").val();
		var fkroomId = parent.parent.$("#roomId").val();
		var eventType = $("#eventAddForm [name='eventType']").val();
		var eventDate = $("#eventAddForm input[name='eventDate']").val();
		var changeRemark = $("#eventAddForm [name='changeRemark']").val();
		if (eventDate == null || eventDate == "") {
			layer.close(eventi);
			layer.alert("发生时间不能为空", {
				shade : [ 0.2, '#fff' ]
			});
			layer.close(eventi);
			return null;
		}
		var eventContent = $("#eventAddForm [name='eventContent']").val();
		var eventProcess = $("#eventAddForm [name='eventProcess']").val();
		var eventParticipant = $("#eventAddForm input[name='eventParticipant']").val();
		var eventId = $("#eventAddForm input[name='eventId']").val();
		//物件隐藏域值
		var AccessoryCaList = $("#eventRecord_articleId ").val();
		if (optEvent == "2" && changeRemark == "") {
			layer.msg("变更说明不能为空");
			$("#eventAddForm [name='changeRemark']").focus();
			layer.close(eventi);
			return null;
		}
		return {
			id : eventId,
			fkcustomerId : fkcustomerId,
			fkroomId : fkroomId,
			eventType : eventType,
			eventDate : eventDate,
			eventContent : eventContent,
			eventProcess : eventProcess,
			eventParticipant : eventParticipant,
			AccessoryCaList : AccessoryCaList,
			opt : optEvent,
			rFileList : rFileList.toString(),
			changeRemark : changeRemark
		}

	}
	//上传成功回调
	function eventAddSuccess(data, status) {
		layer.close(eventi);
		if (status == "success") {
			layer.alert("保存成功！", {
				offset : '20%',
				shade : [ 0.1, '#fff' ]
			}, function() {
				layer.close(eventi);
				parent.location.reload();
			//layer.close(eventi);
			});
		} else {
			layer.alert('保存失败！');
		}
	}
</script>
<body>
	<form class="cmxform" id="eventAddForm" method="post"
		action="customer/event/addEvent">
		<div>
			<!-- 上传文件返回的附件id -->
			<input type="text" id="eventRecord_articleId"
				name="eventRecord_articleId" value="" hidden="hidden"
				style="display: none;" />
			<div class="panel panel-default">
				<div class="panel-body ">
					<table cellpadding="10" cellspacing="5" class="mytable">
						<tr>
							<td width="73"  class="td-right">事件类型</td>
							<td><select class="js-example-basic-single" name="eventType"
								id="eventType" style="width:160px;">
									<option value="">请选择</option>
							</select></td>
							<td width="73"  class="td-right">发生时间</td>
							<td><input name="eventDate" id="eventDate" class="Wdate"
								readonly="readonly"
								onclick="WdatePicker({ dateFmt: 'yyyy-MM-dd'})"></td>
						</tr>

						<tr>
							<td width="73" class="td-right">事件描述</td>
							<td><textarea rows="3" cols="80" style="width:70%;"
									id="eventContent" name="eventContent"></textarea></td>
							<td width="73" class="td-right">处置过程</td>
							<td><textarea rows="3" cols="80" style="width:70%;"
									id="eventProcess" name="eventProcess"></textarea></td>
						</tr>
						<tr>
							<td width="73" class="td-right">参与人</td>
							<td colspan="3"><input name="eventParticipant"
								id="eventParticipant"></td>
						</tr>
						<tr>
							<td width="73" class="td-right">上传资料</td>
							<td colspan="3">
								<div id='filePicker_erul' style="display:none;">上传</div>
							</td>
						</tr>
						<tr>
							<!--上传资料 用来显示缩略图 -->
							<td colspan="4">
								<div id='list_erul'></div>
							</td>
						</tr>


					</table>
					<div id="changeRemark" hidden="hidden">
						<span
							style="display: inline-block;width: 73px;text-align: center;">变更说明</span>
						<textarea rows="3" cols="70" style="width: 65%"
							name="changeRemark"></textarea>
						<span style="color:red"> *</span>
					</div>
				</div>

			</div>
			<div style="width:100%;text-align:center; padding-top:20px">
				<div id="btn_div1" style="display:none;">
					<button class="btn btn-danger radius my-btn" onclick="saveEvent(0)"
						type="button">提交</button>
					<button class="btn btn-danger radius my-btn" onclick="saveEvent(1)"
						type="button">保 存</button>
					<a class="btn btn-default radius"
						onClick="resetForms('eventAddForm');"> 重置 </a> <br />
				</div>
				<div id="btn_div2" style="display:none;">
					<!-- <button id="modifyBtn" type="button"
						class="btn btn-danger radius my-btn" onclick="saveEvent(3)">启用编辑</button> -->
					<button class="btn btn-danger radius my-btn" onclick="saveEvent(2)"
						type="button">提交审批</button>
					<button class="btn btn-default radius" type="button"
						onClick="resetForms('eventAddForm');">重置</button>
				</div>
			</div>
			<input name="eventId" id="entityId_eventId" hidden="hidden"
				value="${eventId }">
		</div>
	</form>
	<script type="text/javascript">
		$(function() {
			//$("div[id^='filePicker_']").css("display", "inline-block");
		});
		//上传成功回调
		function planStageSuccess(file, response) {
			//图片放大初始化
			PostbirdImgGlass.init({
				domSelector : "td img",
				animation : true,
				myDocument : window.parent.parent.parent
			});
		//alert("上传成功回调");
		}
		//上传失败回调
		function planStageErr(file, data) {
			//alert("上传失败回调");
		}
	</script>
</body>